import uuid

from django.db import models

from apps.Base_app.models import BaseModels


class UserModels(BaseModels):
    user_name = models.CharField("用户名", max_length=20, default="未设置用户名")
    pass_word = models.CharField("用户密码", max_length=300)
    phone = models.CharField("电话", max_length=30)
    avatar = models.ImageField('头像', null=True, upload_to='img', blank=True)

    class Meta:
        verbose_name = "用户信息"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.user_name

    @staticmethod
    def UserPhoneHaves(UserPhone):
        try:
            return UserModels.objects.get(phone=UserPhone)
        except UserModels.DoesNotExist:
            return False


class UserTokenModel(BaseModels):
    user = models.ForeignKey(UserModels, on_delete=models.CASCADE, verbose_name="用户信息")
    UserToken = models.CharField("用户token值", max_length=255, null=True)
    IsDelete = models.BooleanField("用户是否在线", default=True)
    ipAddress = models.GenericIPAddressField("用户IP地址", default="127.0.0.1")
    # Contact = models.CharField("用户设备", max_length=50, default="")

    class Meta:
        verbose_name = "用户登录信息"
        verbose_name_plural = verbose_name

    @classmethod
    def get_user_token(cls, user_token):
        try:
            return UserTokenModel.objects.filter(IsDelete=True).get(UserToken=user_token)
        except UserTokenModel.DoesNotExist:
            return False

    @staticmethod
    def create_user_token():
        return uuid.uuid4().__str__()
